package com.vivo.ic.dm;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

import android.os.Build;
import android.os.Environment;
import android.text.TextUtils;
import android.text.format.DateUtils;

/**
 *
 */
public class Constants {
    public static String PRE_TAG = "ICDM-";
    /**
     * 编译时会写入git的commitId号
     */
    public static final String VERSION = "commit 579b415";
    
    public static final ThreadFactory sMultiDownloadThreadFactory = new ThreadFactory() {
        private final AtomicInteger mCount = new AtomicInteger(1);

        public Thread newThread(Runnable r) {
            return new Thread(r, "MultiDownloadThread #" + mCount.getAndIncrement());
        }
    };
    
    /**
     * 默认下载路径名
     */
    public static final String DEFAULT_DL_PARENT = Environment.getDataDirectory() + "/DMDownload";

    /**
     * 默认下载文件名（不带后缀）
     */
    public static final String DEFAULT_DL_FILENAME = "downloadfile";

    /**
     * 网络切换时是wifi环境
     */
    public static final int CHANGE_STATE_WIFI = 255;

    /**
     * 网络切换时是mobile环境
     */
    public static final int CHANGE_STATE_MOBILE = 254;
    
    /**
     * 网络切换时是无网络
     */
    public static final int CHANGE_STATE_NONE = -1;

    /**
     * 之前已经因为网络切换而暂停掉了
     */
    public static final int CHANGE_STATE_PAUSED = 1;
    
    /**
     * 移动网络勾选不再提示的flag
     */
    public static final String SP_NET_WARN_KEY = "never_remind_ps";

    /**
     * 下载默认连接超时时间
     */
    public static final int DEFAULT_TIMEOUT = (int) (20 * DateUtils.SECOND_IN_MILLIS);
    
    /**
     * 下载默认agent
     */
    public static final String DEFAULT_USER_AGENT;

    static {
        final StringBuilder builder = new StringBuilder();

        final boolean validRelease = !TextUtils.isEmpty(Build.VERSION.RELEASE);
        final boolean validId = !TextUtils.isEmpty(Build.ID);
        final boolean includeModel = "REL".equals(Build.VERSION.CODENAME)
                && !TextUtils.isEmpty(Build.MODEL);

        builder.append("AndroidDownloadManager");
        if (validRelease) {
            builder.append("/").append(Build.VERSION.RELEASE);
        }
        builder.append(" (Linux; U; Android");
        if (validRelease) {
            builder.append(" ").append(Build.VERSION.RELEASE);
        }
        if (includeModel || validId) {
            builder.append(";");
            if (includeModel) {
                builder.append(" ").append(Build.MODEL);
            }
            if (validId) {
                builder.append(" Build/").append(Build.ID);
            }
        }
        builder.append(")");

        DEFAULT_USER_AGENT = builder.toString();
    }

    /**
     * mimetype为apk
     */
    public static final String MIMETYPE_APK = "application/vnd.android.package";

    /**
     * 正在下载的notification id
     */
    public static final int NOTI_ID_DOWNLOAD_ACTIVE = 10000;

    /**
     * 下载完成的notification id
     */
    public static final int NOTI_ID_DOWNLOAD_COMPLETED = 20000;

    /**
     * 网络切换时弹出的警告notification id
     */
    public static final int NOTI_ID_NET_CHANGE_WARNING = 39999;
    
    /**
     * 多线程下载
     */
    public static final int DOWNLOAD_TYPE_MULTI = 1; 
    
    
    
    // ------------------------protected 分割线 -----------------------------
    
    /**
     * html类型文件默认的后缀
     */
    static final String DEFAULT_DL_HTML_EXTENSION = ".html";

    /**
     * txt类型文件默认的后缀
     */
    static final String DEFAULT_DL_TEXT_EXTENSION = ".txt";

    /**
     * 未知类型文件默认的后缀
     */
    static final String DEFAULT_DL_BINARY_EXTENSION = ".bin";

    static final String FILENAME_SEQUENCE_SEPARATOR = "-";
    
    static final int BUFFER_SIZE = 8192;

    static final int MIN_PROGRESS_STEP = 4096;

    static final long MIN_PROGRESS_TIME = 1000;

    /**
     * 最大尝试次数
     */
    static final int MAX_RETRIES = 5;

    static final int MIN_RETRY_AFTER = 30; // 30秒

    static final int MAX_RETRY_AFTER = 24 * 60 * 60; // 24小时

    /**
     * 一些下载错误是有希望通过重试来恢复的
     */
    static final int RETRY_FIRST_DELAY = 10; // 10秒
    
    /**
     * 最大重定向数
     */
    static final int MAX_REDIRECTS = 7;
}
